//
//  BacWebView.swift
//  AliBaiChuan
//
//  Created by 郑东喜 on 2017/2/23.
//  Copyright © 2017年 郑东喜. All rights reserved.
//

import UIKit

class BacWebView: BaseWebView {
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        MemUsed.availableMemory()
        
        print("\((#file as NSString).lastPathComponent):(\(#line))\n",MemUsed.usedMemory())
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.navigationItem.leftBarButtonItem = backSEL
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        print("\((#file as NSString).lastPathComponent):(\(#line))\n",self.navigationController?.childViewControllers.count as Any)
    }
    
   fileprivate lazy var backSEL: UIBarButtonItem = {
    let btn : UIBarButtonItem = UIBarButtonItem.init(title: "返回", style: .plain, target: self, action: #selector(leftBarSEL))
        return btn
    }()
}


extension BacWebView {
    @objc fileprivate func leftBarSEL() -> Void {
        print("\((#file as NSString).lastPathComponent):(\(#line))\n")
        self.weView.goBack()
        
        print("\((#file as NSString).lastPathComponent):(\(#line))\n",self.weView.canGoBack)
        
        if self.weView.canGoBack == false {
            self.navigationController?.popToRootViewController(animated: true)
            self.weView.delegate = nil
            self.weView.removeFromSuperview()
        }
   }

}

extension BacWebView {
    override func webViewDidFinishLoad(_ webView: UIWebView) {
        print("\((#file as NSString).lastPathComponent):(\(#line))\n",self.weView.pageCount)
    }
    
    
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        let urlString = request.url?.absoluteString
        
        if navigationType == .linkClicked || navigationType == .formResubmitted || navigationType.rawValue == 0 || navigationType == .reload {
            _webArray.add(urlString)
        }
        
        
        return true
    }
}
